Imports System

Public Class Form1

 

    Public Class DemoDelegate

 

        ' Declares a delegate for a method that takes in an int and returns a String.

        Delegate Function myDemoDelegate(ByVal myInt As Integer) As [String]

 

        ' Defines some methods to which the delegate can point.

        Public Class myDemoClass

 

            ' Defines an instance method.

            Public Function myStringMethod(ByVal myInt As Integer) As [String]

                If myInt > 0 Then

                    Return "positive"

                End If

                If myInt < 0 Then

                    Return "negative"

                End If

                Return "zero"

            End Function 'myStringMethod

 

            ' Defines a static method.

            Public Shared Function mySignMethod(ByVal myInt As Integer) As [String]

                If myInt > 0 Then

                    Return "+"

                End If

                If myInt < 0 Then

                    Return "-"

                End If

                Return ""

            End Function 'mySignMethod

        End Class 'myDemoClass

 

        Public Sub DemoStart()

 

            ' Creates one delegate for each method.

            Dim myDC As New myDemoClass()

            Dim myD1 As New myDemoDelegate(AddressOf myDC.myStringMethod)

            Dim myD2 As New myDemoDelegate(AddressOf myDemoClass.mySignMethod)

 

            ' Invokes the delegates.

            Console.WriteLine("{0} is {1}; use the sign ""{2}"".", 5, myD1(5), myD2(5))

            Console.WriteLine("{0} is {1}; use the sign ""{2}"".", -3, myD1(-3), myD2(-3))

            Console.WriteLine("{0} is {1}; use the sign ""{2}"".", 0, myD1(0), myD2(0))

 

        End Sub 'Main

 

    End Class 'DemoDelegate

 

   

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 

        Dim dd As New DemoDelegate

        dd.DemoStart()

 

    End Sub

End Class

The output is given below:

 

5 is positive; use the sign "+".

-3 is negative; use the sign "-".

0 is zero; use the sign "".